/**
 * Cs340 Fatboy Mafia Team
 */
package fatboy.tools;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


/**
 * @author Ilya Vinogradov
 *
 */
public class JavadocCutter {
	
	private CutterState state = CutterState.SEEK_SLASH_COPY;
	private String buff = "";
	

	public JavadocCutter(String input, String output) throws IOException {
		FileReader inputStream = null;
		FileWriter outputStream = null;
		
		try {
		    inputStream = new FileReader(input);
		    outputStream = new FileWriter(output);
		
		    int c;
		    while ((c = inputStream.read()) != -1) {
		        decider(c, outputStream);
		    }
		} finally {
		    if (inputStream != null) {
		        inputStream.close();
		    }
		    if (outputStream != null) {
		        outputStream.close();
		    }
		}
	}
	
	private void decider(int symbol, FileWriter outputStream) throws IOException {
		switch (symbol) {
		case '/':
			if(state == CutterState.SEEK_SLASH_COPY) {
				state = CutterState.SEEK_STAR_COPY;
				buff = buff + (char)symbol;
			}
			else if (state == CutterState.SEEK_SLASH_SKIP) {
				state = CutterState.SEEK_SLASH_COPY;
				//buff = buff + symbol;
			}
			else if(state == CutterState.SEEK_SLASH_SKIP || 
					state == CutterState.SEEK_STAR_SKIP) {
				;
			}
			else {
				buff = buff + (char)symbol;
				outputStream.write(buff);
				buff ="";
			}
			break;
			
		case '*':
			if(state == CutterState.SEEK_STAR_COPY) {
				buff = buff + (char)symbol;
				state = CutterState.SEEK_SECOND_STAR_COPY;
			}
			else if (state == CutterState.SEEK_SECOND_STAR_COPY) {
				//found javadoc comment
				buff = "";
				state = CutterState.SEEK_STAR_SKIP;
			}
			else if(state == CutterState.SEEK_STAR_SKIP) {
				//buff = buff + (char)symbol;
				state = CutterState.SEEK_SLASH_SKIP;
			}
			else {
				buff = buff + (char)symbol;
				outputStream.write(buff);
				buff ="";
				state = CutterState.SEEK_SLASH_COPY;
			}
			break;
			
		default:
			if(state == CutterState.SEEK_SECOND_STAR_COPY ||
					state == CutterState.SEEK_SLASH_COPY ||
					state == CutterState.SEEK_STAR_COPY) {
				
				buff = buff + (char)symbol;
				outputStream.write(buff);
				buff ="";
				state = CutterState.SEEK_SLASH_COPY;
			}
			//if we had just a stand alone star inside the Javadoc
			else if(state == CutterState.SEEK_SLASH_SKIP){
				state = CutterState.SEEK_STAR_SKIP;
			}
		}

	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		//get the file names from the command line
		for(int i = 0; i < args.length; i++) {
			
			String outFile = args[i].substring(
					args[i].lastIndexOf('/') + 1, args[i].lastIndexOf('.')) + ".txt";
			
			System.out.printf("Processing: \"%-40s\tto the file: %s\n", args[i] +'"', outFile);
			
			JavadocCutter cut = new JavadocCutter(args[i], outFile);
						
		}
		
		
		

	}

}
